import axios from "axios"; // 引用axios


const request = axios.create({
    baseURL:'/api',
    // baseURL:'http://12345xcx.newhkic.cn',   //上线环境
    timeout: 5000,
});

// 请求前
request.interceptors.request.use(config => {
    return config
}, error => {
    Promise.reject(error)
})
// 请求后
request.interceptors.response.use(
    response => {
        return Promise.resolve(response)
    },
    error => {
        return Promise.reject(error.response)
    }
)
//get请求
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        request
            .get(url, {
                params: params,
            })
            .then((response) => {
                resolve(response);
            })
            .catch((err) => {
                reject(err);
            });
    });
}
//post请求
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        request.post(url, data).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
}
//put请求
export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        request.post(url, data).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
}
//post请求
export function del(url, data = {}) {
    return new Promise((resolve, reject) => {
        request.delete(url, data).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
}
export default {
    get,
    post,
    put,
    del
}
